package controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import service.UserService;
import controller.form.Form;
import core.controller.AbstractController;
import core.ext.direct.annotations.ExtJsDirectRemotingAnnotations;
import domain.entity.User;

/**
 * 
 * @author mansari
 */
@Controller
@RequestMapping(value = "/extjs/remoting")
//public class DemoExtJsDirectRemotingController extends
//		ExtJsDirectRemotingController implements MessageSourceAware {
public class DemoExtJsDirectRemotingController extends AbstractController{
	
	public static final Logger logger = Logger.getLogger(DemoExtJsDirectRemotingController.class);
	
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	private MessageSource messageSource;

	public DemoExtJsDirectRemotingController() {
	}

	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}

	@ExtJsDirectRemotingAnnotations.ExtJsRemotingMethod
	public Long multiply(String num) {
		return Long.valueOf(num) * 8;
	}

	@ExtJsDirectRemotingAnnotations.ExtJsRemotingMethod
	public String doEcho(String message) {
		User u = new User();
		u.setLoginName("kelvin");
		return message;
	}

	@ExtJsDirectRemotingAnnotations.ExtJsRemotingMethod
	public List<ExtJsTreeNode> getTree(String node) {// bug in ExtJs 3.2.1 and
														// prior requires
														// treeload method to
														// accept
														// at least one
														// parameter or UI node
														// which was expanded
														// keeps
														// spinning with loading
														// animation

		List<ExtJsTreeNode> list = new ArrayList<ExtJsTreeNode>();
		list.add(new ExtJsTreeNode("n1", "Node 1", true));
		list.add(new ExtJsTreeNode("n2", "Node 2", true));
		list.add(new ExtJsTreeNode("n3", "Node 3", true));
		return list;
	}

	@ExtJsDirectRemotingAnnotations.ExtJsRemotingMethod(formLoad = true)
	public Form loadForm(String name, String email) {
		Form form = new Form();
		form.setName(name);
//		form.setEmail(email);
		return form;
	}

	@ExtJsDirectRemotingAnnotations.ExtJsRemotingMethod
	@RequestMapping(value = "/updateForm", method = RequestMethod.POST)
	public List<ExtJsTreeNode> updateForm(Locale locale, HttpServletRequest request,
			@Valid Form form, BindingResult result) {
//		EntityManager em = userService.getDao().getEntityManager();
//		em.getTransaction().begin();
		String message = "successful";
		if (!result.hasErrors()) {
//			message = this.messageSource.getMessage("success.message", null,
//					locale);
		}
		logger.info("Start to write user.");
		User u = new User();
		u.setLoginName(form.getLoginName());
		u.setName(form.getName());
		u.setPassword(form.getPassword());
		u.setRoles(form.getRoles());
		u.setSalt(form.getSalt());
		u.setRegisterDate(new Date());
		userService.store(u);
		ModelAndView mnv = new ModelAndView();
		mnv.addObject("user", new ExtJsTreeNode("n3", "Node 3", true));
		List<ExtJsTreeNode> arrayList = new ArrayList<ExtJsTreeNode>();
		ExtJsTreeNode m = new ExtJsTreeNode("n3", "Node 3", true);arrayList.add( m);
		logger.info("Write user complete.");
		return arrayList;
	}
}

class ExtJsTreeNode {

	private String id;
	private String text;
	private boolean leaf;

	public ExtJsTreeNode(String id, String text, boolean leaf) {
		this.text = text;
		this.leaf = leaf;
		this.id = id;
	}

	public boolean isLeaf() {
		return leaf;
	}

	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}